#ifndef AC_COMMON_LOCK_HPP_
#define AC_COMMON_LOCK_HPP_

#include <cassert>

#define LOCK(l) const Lock the_lock(l);

class Mutex
{
public:
    Mutex() {}
    ~Mutex() {}
    void lock() {}
    void unlock() {}
private:
    Mutex(const Mutex&);
    void operator = (const Mutex&);
};

class Lock
{
public:
    Lock(Mutex* l)
        : lock_(l)
    {
        if (lock_)
            lock_->lock();
    }

    void set(Mutex* l)
    {
        assert(!lock_);
        lock_ = l;
        if (lock_)
            lock_->lock();
    }

    void release()
    {
        if (lock_)
            lock_->unlock();
        lock_ = NULL;
    }

    ~Lock()
    {
        if (lock_)
            lock_->unlock();
    }

private:
    Lock(const Lock&);
    void operator = (const Lock&);
    Mutex* lock_;
};


#endif  // AC_COMMON_LOCK_HPP_
